package cn.haiway.drools.execute.rest;

import cn.haiway.drools.execute.dto.ParamDTO;
import cn.haiway.drools.execute.rest.req.ParamRequest;
import cn.haiway.drools.execute.rest.response.ExecuteResponse;
import cn.haiway.drools.execute.service.DroolsExecuteService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author Haiway  on 2019/4/17
 */
@RestController
@RequestMapping(value = BaseRestController.BASE_PREFIX_URL_V1)
public class ExecutionController extends BaseRestController{

    @Resource
    DroolsExecuteService droolsExecuteService;

    @PostMapping("/execute")
    public  ExecuteResponse excute(@Valid @RequestBody ParamRequest request) throws Exception{
        ParamDTO dto = new ParamDTO();
        BeanUtils.copyProperties(request,dto);

        ExecuteResponse response = new ExecuteResponse();
        Map<String,Object> mp = droolsExecuteService.executeRule(dto);
        response.setProject(dto.getProject());
        response.setResult(mp);

        return  response;
    }

}
